package com.yznote.cloudesk.user.server.impl;

import com.yznote.cloudesk.common.pojo.Notice;
import com.yznote.cloudesk.common.pojo.User;
import com.yznote.cloudesk.common.tokenShare.YTokenShare;
import com.yznote.cloudesk.common.utils.IdUtils;
import com.yznote.cloudesk.user.mapper.NoticeMapper;
import com.yznote.cloudesk.user.server.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    YTokenShare yTokenShare;
    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public Boolean noticeUser(Notice notice) {
        User user = yTokenShare.get();
        if (user != null) {
            notice.setCreateBy(user.getUserId());
            notice.setUpdateBy(user.getUserId());
            notice.setNoticeType(0);
            __initNotice(notice);
        } else {
            notice.setCreateBy(-1l);
            notice.setUpdateBy(-1l);
        }
        Integer i = noticeMapper.insertNotice(notice);
        if (i == 0) return false;
        return true;
    }

    @Override
    public List<Notice> pullNotice() {
        User user = yTokenShare.get();
        List<Notice> noticeList = noticeMapper.pullNotice(user.getUserId());
        return noticeList;
    }

    private void __initNotice(Notice notice) {
        LocalDateTime now = LocalDateTime.now();
        notice.setNoticeTime(now);
        notice.setCreateTime(now);
        notice.setUpdateTime(now);
        notice.setNoticeId(IdUtils.nextId());
    }
}
